/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.message.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.message.dao.SystemNotifyMessageDetailDao;
import com.chlitina.store.modules.message.entity.SystemNotifyMessage;
import com.chlitina.store.modules.message.entity.SystemNotifyMessageDetail;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 系统提示信息Service
 * @author lvbo
 * @version 2013-07-16
 */
@Component
@Transactional(readOnly = true)
public class SystemNotifyMessageDetailService extends BaseService {

	@Autowired
	private SystemNotifyMessageDetailDao systemNotifyMessageDetailDao;
	
	public SystemNotifyMessageDetail getMsgById(Long messageId) {
		Long storeId = StoreUtils.getStore().getId();
		return systemNotifyMessageDetailDao.findByPKId(storeId, messageId);
	}
	
	public Page<SystemNotifyMessageDetail> find(Page<SystemNotifyMessageDetail> page, SystemNotifyMessageDetail systemNotifyMessageDetail, int type) {
		page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
		DetachedCriteria dc = systemNotifyMessageDetailDao.createDetachedCriteria();
		if (systemNotifyMessageDetail.getStore() != null && systemNotifyMessageDetail.getStore().getId() != null) {
			dc.add(Restrictions.eq("store.id", systemNotifyMessageDetail.getStore().getId()));
		}
		dc.createAlias("systemNotifyMessage", "msg");
		Date now = new Date();
		dc.add(Restrictions.ge("msg.endDate", now));
		dc.add(Restrictions.le("msg.beginDate", now));
		dc.add(Restrictions.eq("msg.type", type));
		dc.addOrder(Order.desc("msg.beginDate"));
		return systemNotifyMessageDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SystemNotifyMessageDetail systemNotifyMessageDetail) {
		systemNotifyMessageDetailDao.save(systemNotifyMessageDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long msgId) {
		SystemNotifyMessageDetail messageDetail = getMsgById(msgId);
		systemNotifyMessageDetailDao.delete(messageDetail);
	}
	@Transactional(readOnly = false)
	public void changeStatus(long msgId) {
		SystemNotifyMessageDetail messageDetail = getMsgById(msgId);
		int status = messageDetail.getStatus();
		if (status == SystemNotifyMessageDetail.READ_STATUS) {
			messageDetail.setStatus(SystemNotifyMessageDetail.UNREAD_STATUS);
		}
		if (status == SystemNotifyMessageDetail.UNREAD_STATUS) {
			messageDetail.setStatus(SystemNotifyMessageDetail.READ_STATUS);
		}
		save(messageDetail);
	}
	
	@Transactional(readOnly = false)
	public List<SystemNotifyMessageDetail> findUnreadMsg(Long storeId, Date date ) {
		DetachedCriteria dc = systemNotifyMessageDetailDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store.id", storeId));
		dc.createAlias("systemNotifyMessage", "msg");
		dc.add(Restrictions.ge("msg.endDate", date));
		dc.add(Restrictions.le("msg.beginDate", date));
		dc.add(Restrictions.eq("msg.type", SystemNotifyMessage.COMPANY_TYPE));
		dc.add(Restrictions.eq("status", SystemNotifyMessageDetail.UNREAD_STATUS));
		return systemNotifyMessageDetailDao.find(dc);
	}
	
}
